package com.linwen.javase;

import java.util.Objects;

/**
 * @author : linwen
 * @date : 下午10:21 2021/1/5
 */
public class _Compare {
    public static void main(String[] args) {
        CompareObj p1 = new CompareObj(12.3);
        CompareObj p2 = new CompareObj(12.301);
        System.out.println(p1.compareTo(p2));
    }
}
class CompareObj implements Comparable{
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof CompareObj)) return false;
        CompareObj that = (CompareObj) o;
        return Double.compare(that.age, age) == 0;
    }

    @Override
    public int hashCode() {
        return Objects.hash(age);
    }

    private double age;

    public double getAge() {
        return age;
    }

    public void setAge(double age) {
        this.age = age;
    }

    public CompareObj(double age) {
        this.age = age;
    }

    @Override
    public int compareTo(Object o) {
        if(this==o){
            return 0;
        }
        if(o instanceof CompareObj){
            if(this.age>((CompareObj)o).age){
                return 1;
            }else if(this.age==((CompareObj)o).age){
                return 0;
            }else{
                return -1;
            }
        }else{
            try {
                throw new Exception("传入的对象类型不匹配！！");
            } catch (Exception e) {
                //e.printStackTrace();
                System.out.println("系统异常");
            }
        }
        return 0;
    }
}
